/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Facades;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.persistence.EntityManager;
import javax.persistence.Query;

/**
 *
 * @author Cristian
 */
public abstract class AbstractFacade<T> {
    private Class<T> entityClass;

    public AbstractFacade(Class<T> entityClass) {
        this.entityClass = entityClass;
    }

    protected abstract EntityManager getEntityManager();

    public void create(T entity) {
        getEntityManager().persist(entity);
    }

    public void edit(T entity) {
        getEntityManager().merge(entity);
    }

    public void remove(T entity) {
        getEntityManager().remove(getEntityManager().merge(entity));
    }

    public T find(Object id) {
        return getEntityManager().find(entityClass, id);
    }

    public List<T> findAll() {
        javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
        cq.select(cq.from(entityClass));
        return getEntityManager().createQuery(cq).getResultList();
    }

    //método genérico para buscar por id Creada por Cristian Morales A.
       public List<T> findbyCampo(T entity,String campo){
   //     Query q = getEntityManager().createQuery("Select o from " + entityClass.getSimpleName() + " o WHERE o."+campo+"=2");
     Query q = getEntityManager().createQuery("Select i from " + entityClass.getSimpleName() + " i WHERE i."+campo+"= :"+campo);
    
     q.setParameter(campo,entity);
        
        return q.getResultList();
    }
       
       
   //método genérico para buscar por id Creada por Cristian Morales A.
       public List<T> findbyFacultadID(int id,String campo){
   //     Query q = getEntityManager().createQuery("Select o from " + entityClass.getSimpleName() + " o WHERE o."+campo+"=2");
     Query q = getEntityManager().createQuery("Select i from " + entityClass.getSimpleName() + " i WHERE i."+campo+"= :"+campo);
    
     Entities.Scittbfadece entidad=new Entities.Scittbfadece(id);
     
     q.setParameter(campo,entidad);
        
        return q.getResultList();
    }    
       
       public String findbyDirectorio(int id,String campo){
           String directorio="";
           Entities.Directorios obj=new Entities.Directorios();
            //Query q = getEntityManager().createQuery("Select o from " + entityClass.getSimpleName() + " o WHERE o."+campo+"=2");
            Query q = getEntityManager().createQuery("Select i from " + entityClass.getSimpleName() + " i WHERE i."+campo+"= :"+campo);
            q.setParameter(campo,id);
            List<Object[]> result = q.getResultList();
            Iterator iterador = result.listIterator();
            //directorio=(String)result[0];
            while (iterador.hasNext()) {
             obj = (Entities.Directorios)iterador.next();
            }
            directorio=obj.getDirectorio();
   return directorio;
    } 
       
//       public static void main(String[] args)
//       {
//      
//       }
       
    public List<T> findRange(int[] range) {
        javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
        cq.select(cq.from(entityClass));
        javax.persistence.Query q = getEntityManager().createQuery(cq);
        q.setMaxResults(range[1] - range[0]);
        q.setFirstResult(range[0]);
        return q.getResultList();
    }

    public int count() {
        javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
        javax.persistence.criteria.Root<T> rt = cq.from(entityClass);
        cq.select(getEntityManager().getCriteriaBuilder().count(rt));
        javax.persistence.Query q = getEntityManager().createQuery(cq);
        return ((Long) q.getSingleResult()).intValue();
    }
    
}
